﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LuaMemCheck
{
    public partial class LeakForm : Form
    {
        private List<LeakLuaObject> _leakLuaObjects = new List<LeakLuaObject>();
        private Dictionary<int, ListViewItemComparer> _comparerDic = new Dictionary<int, ListViewItemComparer>
        {
            { 0, new ListViewDoubleComparer(0)},
            { 1, new ListViewStringComparer(1)},
            { 2, new ListViewStringComparer(2)},
            { 3, new ListViewDoubleComparer(3)},
            { 4, new ListViewStringSplitComparer(4)},
            { 5, new ListViewStringSplitComparer(5)},
            { 6, new ListViewStringSplitComparer(6)},
        };
        public LeakForm()
        {
            InitializeComponent();
        }


        public void SetLeakLuaObjects(List<LeakLuaObject> luaObjects)
        {
            _leakLuaObjects = luaObjects;
        }

        private void LeakForm_Load(object sender, EventArgs e)
        {

            ShowLuaInfoList(_leakLuaObjects);
        }

        private void ShowLuaInfoList(List<LeakLuaObject>  leakLuaObjects)
        {
            LuaInfoList.Clear();
            LuaInfoList.GridLines = true;//表格是否显示网格线
            LuaInfoList.FullRowSelect = true;//是否选中整行

            LuaInfoList.View = View.Details;//设置显示方式
            LuaInfoList.Scrollable = true;//是否自动显示滚动条
            LuaInfoList.MultiSelect = false;//是否可以选择多行

            //添加表头（列）
            LuaInfoList.Columns.Add("序号", 30, HorizontalAlignment.Center);
            LuaInfoList.Columns.Add("地址", 150, HorizontalAlignment.Center);
            LuaInfoList.Columns.Add("链接", 800, HorizontalAlignment.Left);
            LuaInfoList.Columns.Add("1Table2Function", 300, HorizontalAlignment.Left);
            LuaInfoList.Columns.Add("引用数量", 100, HorizontalAlignment.Center);
            LuaInfoList.Columns.Add("孩子数", 100, HorizontalAlignment.Center);
            LuaInfoList.Columns.Add("占用内存", 100, HorizontalAlignment.Center);

            //添加表格内容
            for (int i = 0; i < leakLuaObjects.Count; i++)
            {
                ListViewItem item = new ListViewItem();
                item.SubItems.Clear();

                item.SubItems[0].Text = i.ToString();
                item.SubItems.Add(leakLuaObjects[i].Address);
                item.SubItems.Add(leakLuaObjects[i].NodeStr);
                item.SubItems.Add(leakLuaObjects[i].Type.ToString());
                item.SubItems.Add(leakLuaObjects[i].GetRefStr());
                item.SubItems.Add(leakLuaObjects[i].GetChildCountStr());
                item.SubItems.Add(leakLuaObjects[i].GetSizeStr());
                LuaInfoList.Items.Add(item);
            }

        }

        private void AllShowBtn_Click(object sender, EventArgs e)
        {
            ShowLuaInfoList(_leakLuaObjects);

        }

        private void ChildCountShowBtn_Click(object sender, EventArgs e)
        {
            List<LeakLuaObject> leakLuaObjects = new List<LeakLuaObject>();
            for (int i = 0; i < _leakLuaObjects.Count; i++)
            {
                if (_leakLuaObjects[i].CheckChildCountAdd())
                {
                    leakLuaObjects.Add(_leakLuaObjects[i]);
                }
            }
            ShowLuaInfoList(leakLuaObjects);

        }

        private void LuaInfoList_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            LuaInfoList.ListViewItemSorter = _comparerDic[e.Column];
            LuaInfoList.Sort();
            _comparerDic[e.Column].ChangeSortOrder();
        }

        private void HighRiskBtn_Click(object sender, EventArgs e)
        {
            List<LeakLuaObject> leakLuaObjects = new List<LeakLuaObject>();
            for (int i = 0; i < _leakLuaObjects.Count; i++)
            {
                if (_leakLuaObjects[i].HighRiskCountAdd())
                {
                    leakLuaObjects.Add(_leakLuaObjects[i]);
                }
            }
            ShowLuaInfoList(leakLuaObjects);
        }
    }



    public class ListViewStringSplitComparer : ListViewItemComparer
    {
        public ListViewStringSplitComparer(int column) : base(column)
        {

        }

        public override int Compare(object x, object y)
        {
            int a = ((ListViewItem) x).SubItems[_col].Text.Split('>').Length;
            int b = ((ListViewItem) y).SubItems[_col].Text.Split('>').Length;
            if (_order == SortOrder.Ascending)
                return a < b ? 1 : -1;
            return - (a < b ? 1 : -1);
        }
    }



    public class LuaInfoListControl
    {
        private ListView LuaInfoList;
        private List<LuaObject> _luaObjects;
        private Dictionary<int, ListViewItemComparer> _comparerDic = new Dictionary<int, ListViewItemComparer>
        {
            { 0, new ListViewDoubleComparer(0)},
            { 1, new ListViewStringComparer(1)},
            { 2, new ListViewStringComparer(2)},
            { 3, new ListViewDoubleComparer(3)},
            { 4, new ListViewDoubleComparer(4)},
            { 5, new ListViewDoubleComparer(5)},
            { 6, new ListViewDoubleComparer(6)},
        };

        
        public LuaInfoListControl(ListView luaInfoList)
        {
            LuaInfoList = luaInfoList;
            LuaInfoList.ColumnClick += new ColumnClickEventHandler(LuaInfoList_ColumnClick);

            ContextMenuStrip LuaInfoMenuStrip = new ContextMenuStrip();
            ToolStripItem LuaDetailToolStripMenuItem = new ToolStripButton();

            LuaInfoMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            LuaDetailToolStripMenuItem});
            LuaInfoMenuStrip.Name = "LuaInfoMenuStrip";
            LuaInfoMenuStrip.Size = new System.Drawing.Size(101, 26);

            LuaDetailToolStripMenuItem.Name = "LuaDetailToolStripMenuItem";
            LuaDetailToolStripMenuItem.Size = new System.Drawing.Size(100, 22);
            LuaDetailToolStripMenuItem.Text = "详情";
            LuaDetailToolStripMenuItem.Click += new System.EventHandler(this.LuaDetailToolStripMenuItem_Click);


            LuaInfoList.ContextMenuStrip = LuaInfoMenuStrip;


        }

        public void SetLuaObjects(List<LuaObject> luaObjects)
        {
            _luaObjects = luaObjects;
            ShowLuaInfoList(luaObjects);
        }

        private void ShowLuaInfoList(List<LuaObject> luaObjects)
        {
            LuaInfoList.Clear();
            LuaInfoList.GridLines = true;//表格是否显示网格线
            LuaInfoList.FullRowSelect = true;//是否选中整行

            LuaInfoList.View = View.Details;//设置显示方式
            LuaInfoList.Scrollable = true;//是否自动显示滚动条
            LuaInfoList.MultiSelect = false;//是否可以选择多行

            //添加表头（列）
            LuaInfoList.Columns.Add("序号", 30, HorizontalAlignment.Center);
            LuaInfoList.Columns.Add("地址", 150, HorizontalAlignment.Center);
            LuaInfoList.Columns.Add("链接", 800, HorizontalAlignment.Left);
            LuaInfoList.Columns.Add("1Table2Function", 300, HorizontalAlignment.Left);
            LuaInfoList.Columns.Add("引用数量", 100, HorizontalAlignment.Center);
            LuaInfoList.Columns.Add("孩子数", 100, HorizontalAlignment.Center);
            LuaInfoList.Columns.Add("占用内存", 100, HorizontalAlignment.Center);

            //添加表格内容
            for (int i = 0; i < luaObjects.Count; i++)
            {
                ListViewItem item = new ListViewItem();
                item.SubItems.Clear();

                item.SubItems[0].Text = i.ToString();
                item.SubItems.Add(luaObjects[i].Address);
                item.SubItems.Add(luaObjects[i].NodeStr);
                item.SubItems.Add(luaObjects[i].Type.ToString());
                item.SubItems.Add(luaObjects[i].Ref.ToString());
                item.SubItems.Add(luaObjects[i].ChildCount.ToString());
                item.SubItems.Add(luaObjects[i].Size.ToString());
                LuaInfoList.Items.Add(item);
            }

        }

        private void LuaInfoList_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            LuaInfoList.ListViewItemSorter = _comparerDic[e.Column];
            LuaInfoList.Sort();
            _comparerDic[e.Column].ChangeSortOrder();
        }

        private void LuaDetailToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var selectedItem = this.LuaInfoList.SelectedItems[0];
            LuaObjectDetail luaObjectDetailForm = new LuaObjectDetail();
            SnapInfo curSnapInfo = SnapInfoMgr.Instance.GetCurrentSnapInfo();
            luaObjectDetailForm.SetLuaObject(curSnapInfo.GetLuaObject(selectedItem.SubItems[1].Text), curSnapInfo);
            luaObjectDetailForm.Show();
        }
    }
    
}
